<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="oneHandleOk('two')"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>
          <a-col :span="12" :gutter="8">
            <a-form-item
             :labelCol="labelCol"
             :wrapperCol="wrapperCol"
             label="二级分类名称">
              <a-select placeholder="请选择" :disabled="isDisable" v-decorator="['twoTypeName',{rules:[{required: true,message:'请选择二级药品'}]}]">
                <a-select-option v-for="(item,index) in twoListAry" :key="item.id" :value="item.id">{{ item.twoTypeName }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="药品详情" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="knoDrugInfAttTable.loading"
            :columns="knoDrugInfAttTable.columns"
            :dataSource="knoDrugInfAttTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="isShowChildAdd"
          />
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </a-modal>
</template>

<script>

  import moment from 'moment'
  import pick from 'lodash.pick'
  import { FormTypes } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { getAction } from '@/api/manage'

  export default {
    name: 'KnoDrugTwoTypeModal',
    mixins: [JEditableTableMixin],
    data() {
      return {
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        refKeys: ['knoDrugInfAtt', 'knoDrugTwoType'],
        activeKey: 'knoDrugInfAtt',
        validatorRules: {
        },
        refKeys: ['knoDrugInfAtt', ],
        activeKey: 'knoDrugInfAtt',
        
        // 药品信息子表
        knoDrugInfAttTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '药品通用名称',
              key: 'drugsGenName',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '药品英文名称',
              key: 'drugsEngName',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '药品汉语拼音',
              key: 'drugsPinyin',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '药品成份',
              key: 'drugCom',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '药品性状',
              key: 'drugPro',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '适用症',
              key: 'applicable',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '规格',
              key: 'specs',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '用法用量',
              key: 'usageDosage',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '不良反应',
              key: 'adverseReactions',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '禁忌',
              key: 'taboo',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '注意事项',
              key: 'carefulMatter',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '药物相互作用',
              key: 'interation',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '药物过量',
              key: 'drugsOverdose',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '贮藏',
              key: 'sto',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '包装',
              key: 'packing',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '有效期',
              key: 'validityTerm',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '执行标准',
              key: 'executeStandard',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '批准文号',
              key: 'approvalNumber',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            {
              title: '生产企业名称',
              key: 'companyName',
              type: FormTypes.input,
              defaultValue: '',
              placeholder: '请输入${title}',
            },
            
          ]
        },
        
        isShowChildAdd: true,//是否显示子信息的新增
        url: {
          add: "/knowledge/knoDrugTwoType/add",
          edit: "/knowledge/knoDrugTwoType/edit",
          knoDrugInfAtt: {
            list: '/knowledge/knoDrugTwoType/queryKnoDrugInfAttByMainId'
          },
          getTwoList:'/knowledge/knoDrugInfOwner/getTwoAll',
        },
        twoListAry: [],
        isDisable: false,
      }
    },
    created() {
      //获取一级药品列表
      getAction(this.url.getTwoList,{searchType: 3,catetoryType: 1}).then((res) => {
        if (res.success) {
          this.twoListAry = res.result.records;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    methods: {
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        if(this.model.id == undefined){
          this.isDisable = false;
          this.isShowChildAdd = true;
        }else{
          this.isDisable = true;
          this.isShowChildAdd = false;
        }
        console.log("editAfter:::"+JSON.stringify(this.model));
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'twoTypeName', 'operator', 'opeType', 'opeDate', 'drugInfOwnerId', ))
          // 时间格式化
          this.form.setFieldsValue({ opeDate: this.model.opeDate ? moment(this.model.opeDate) : null })
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id };
          this.requestSubTableData(this.url.knoDrugInfAtt.list, params, this.knoDrugInfAttTable)
        }
      },
 
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        //时间格式化
        // main.opeDate = main.opeDate ? main.opeDate.format('YYYY-MM-DD HH:mm:ss') : null;
        return {
          ...main, // 展开
          knoDrugInfAttList: allValues.tablesValue[0].values,
        }
      }
    }
  }
</script>

<style scoped>
</style>